package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.ITradingDataDAL;
import com.qire.manhua.model.enumeration.DetailType;
import com.qire.manhua.model.viewModel.impl.AccountDetailCoinViewModel;
import com.qire.manhua.model.vo.CoinExpenditureRecordVO;
import com.qire.manhua.model.vo.CoinIncomeRecordVO;

public class AccountDetailCoinService extends DataService {

    private final ITradingDataDAL tradingDataDAL = WarehouseFactory.create(this, ITradingDataDAL.class);

    private final AccountDetailCoinViewModel viewModel;

    public AccountDetailCoinService(AccountDetailCoinViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 通过类型获取取明细记录
     * @param type 明细记录类型：支出、收入、过期
     * @param pageNo 页号
     */
    public void fetchCoinDetailRecordBy(DetailType type, int pageNo) {
        switch (type) {
            case EXPENDITURE:
                tradingDataDAL.fetchCoinExpenditureRecord(pageNo);
                return;
            case INCOME:
                tradingDataDAL.fetchCoinIncomeRecord(pageNo);
                return;
        }
    }
    @StrategyAnnotation(event = Url.CoinExpenditureRecordData)
    public void fetchCoinExpenditureRecordApiCallback(CoinExpenditureRecordVO[] dataVOs, boolean isCache) {
        viewModel.refreshLayoutFinish(300);

        if(ObjectUtil.isNull(dataVOs)) {
            return;
        }

        for(CoinExpenditureRecordVO vo : dataVOs) {
            CharSequence title = vo.title();
            CharSequence description = vo.sub_title;
            CharSequence recordTime = vo.create_time;
            CharSequence coinValue = vo.coin_bean;
            viewModel.addDetailRecordItem(title, description, recordTime, coinValue);
        }
        viewModel.refreshDetailRecordAdapter();

        if (!isCache) {
            viewModel.nextPage();
        }
    }
    @StrategyAnnotation(event = Url.CoinIncomeRecordData)
    public void fetchCoinIncomeRecordApiCallback(CoinIncomeRecordVO[] dataVOs, boolean isCache) {
        viewModel.refreshLayoutFinish(300);

        if(ObjectUtil.isNull(dataVOs)) {
            return;
        }

        for(CoinIncomeRecordVO vo : dataVOs) {
            CharSequence title = vo.title;
            CharSequence description = "";
            CharSequence recordTime = vo.create_time;
            CharSequence coinValue = vo.coin_bean;
            viewModel.addDetailRecordItem(title, description, recordTime, coinValue);
        }
        viewModel.refreshDetailRecordAdapter();

        if (!isCache) {
            viewModel.nextPage();
        }
    }

}
